/*     */ package com.zimbra.cs.mailclient.imap;
/*     */ 
/*     */ import com.zimbra.common.zmime.ZSharedFileInputStream;
/*     */ import java.io.DataInputStream;
/*     */ import java.io.File;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.OutputStream;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import javax.mail.util.SharedByteArrayInputStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class Literal
/*     */   extends ImapData
/*     */ {
/*     */   private byte[] bytes;
/*     */   private final File file;
/*     */   private final InputStream stream;
/*     */   private final int size;
/*     */   private boolean tmp;
/*     */   
/*     */   public Literal(byte[] bytes)
/*     */   {
/*  41 */     this.bytes = bytes;
/*  42 */     this.file = null;
/*  43 */     this.stream = null;
/*  44 */     this.size = bytes.length;
/*     */   }
/*     */   
/*     */   public Literal(File file, boolean tmp) {
/*  48 */     this.file = file;
/*  49 */     this.stream = null;
/*  50 */     this.size = ((int)file.length());
/*  51 */     this.tmp = tmp;
/*     */   }
/*     */   
/*     */   public Literal(File file) {
/*  55 */     this(file, false);
/*     */   }
/*     */   
/*     */   public Literal(InputStream is, int size) {
/*  59 */     this.bytes = null;
/*  60 */     this.file = null;
/*  61 */     this.stream = is;
/*  62 */     this.size = size;
/*     */   }
/*     */   
/*     */   public ImapData.Type getType()
/*     */   {
/*  67 */     return ImapData.Type.LITERAL;
/*     */   }
/*     */   
/*     */   public InputStream getInputStream() throws IOException
/*     */   {
/*  72 */     if (this.bytes != null)
/*  73 */       return new SharedByteArrayInputStream(this.bytes);
/*  74 */     if (this.file != null) {
/*  75 */       return new ZSharedFileInputStream(this.file);
/*     */     }
/*  77 */     return this.stream;
/*     */   }
/*     */   
/*     */ 
/*     */   public int getSize()
/*     */   {
/*  83 */     return this.size;
/*     */   }
/*     */   
/*     */   public File getFile() {
/*  87 */     return this.file;
/*     */   }
/*     */   
/*     */   public byte[] getBytes() throws IOException
/*     */   {
/*  92 */     if (this.bytes != null) return this.bytes;
/*  93 */     DataInputStream is = new DataInputStream(getInputStream());
/*     */     try {
/*  95 */       byte[] b = new byte[this.size];
/*  96 */       is.readFully(b);
/*  97 */       return b;
/*     */     } finally {
/*  99 */       is.close();
/*     */     }
/*     */   }
/*     */   
/*     */   public void writePrefix(ImapOutputStream os, boolean lp) throws IOException {
/* 104 */     os.write(123);
/* 105 */     os.write(String.valueOf(this.size));
/* 106 */     if (lp) os.write(43);
/* 107 */     os.writeLine("}");
/*     */   }
/*     */   
/*     */   public void writeData(OutputStream os) throws IOException {
/* 111 */     if (this.bytes != null) {
/* 112 */       os.write(this.bytes);
/*     */     } else {
/* 114 */       InputStream is = getInputStream();
/*     */       try {
/* 116 */         byte[] b = new byte['ࠀ'];
/*     */         int len;
/* 118 */         while ((len = is.read(b)) != -1) {
/* 119 */           os.write(b, 0, len);
/*     */         }
/*     */       } finally {
/* 122 */         is.close();
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 129 */     if (this.stream != null) {
/* 130 */       return String.format("[literal %d bytes]", new Object[] { Integer.valueOf(this.size) });
/*     */     }
/*     */     try {
/* 133 */       return new String(getBytes(), "UTF-8");
/*     */     } catch (UnsupportedEncodingException e) {
/* 135 */       throw new InternalError("UTF-8 charset not found");
/*     */     } catch (IOException e) {
/* 137 */       throw new IllegalStateException("I/O error while reading literal bytes", e);
/*     */     }
/*     */   }
/*     */   
/*     */   public void dispose()
/*     */   {
/* 143 */     if (this.stream != null) {
/*     */       try {
/* 145 */         this.stream.close();
/*     */       }
/*     */       catch (IOException e) {}
/*     */     }
/*     */     
/* 150 */     if ((this.file != null) && (this.tmp)) {
/* 151 */       this.file.delete();
/*     */     }
/*     */   }
/*     */   
/*     */   public void finalize() throws Throwable
/*     */   {
/* 157 */     super.finalize();
/* 158 */     dispose();
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/imap/Literal.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */